package com.wing.bill.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.bill.model.entity.BillDetailInfo;
import com.wing.bill.model.enums.BillStatusEnum;
import com.wing.bill.model.request.WarehouseBillDetailForm;
import com.wing.bill.model.response.WarehouseBillDetailVO;
import com.wing.common.utils.DateUtils;
import com.wing.common.utils.PageResult;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.bill.model.entity.WarehouseBillDetail;
import com.wing.bill.dao.WarehouseBillDetailDao;
import org.springframework.transaction.annotation.Transactional;

@Service
public class WarehouseBillDetailServiceImpl extends ServiceImpl<WarehouseBillDetailDao, WarehouseBillDetail> implements WarehouseBillDetailService{

    @Override
    public boolean save(WarehouseBillDetailForm warehouseBillDetailForm) {
        WarehouseBillDetail warehouseBillDetail = new WarehouseBillDetail();
        warehouseBillDetail.setCustomerId(warehouseBillDetailForm.getCustomerId());
        warehouseBillDetail.setSysBundleCode(warehouseBillDetailForm.getSysBundleCode());
        warehouseBillDetail.setBillPeriod(DateUtils.getYearMonth());
        warehouseBillDetail.setStartDate(LocalDateTime.now());
        warehouseBillDetail.setBillStatus(BillStatusEnum.UNPAID.getCode());
        warehouseBillDetail.setBillPrice(warehouseBillDetailForm.getBillPrice());
        return this.save(warehouseBillDetail);
    }

    @Override
    public Boolean update(WarehouseBillDetailForm warehouseBillDetailForm){
        WarehouseBillDetail warehouseBillDetail = this.getById(warehouseBillDetailForm.getId());
        warehouseBillDetail.setBillStatus(BillStatusEnum.PAID.getCode());
        return this.updateById(warehouseBillDetail);
    }

    @Override
    public PageResult<Page<WarehouseBillDetailVO>> pageList4app(Page<WarehouseBillDetail> pg, Map paramMap) {
        return new PageResult<>(baseMapper.list4app(pg, paramMap));
    }

    @Override
    public Boolean updateBillDetail(Long customerId, List<String> sysBundleCodeList){
        // 查询本月账单
        List<WarehouseBillDetail> warehouseBillDetailList = baseMapper
                .selectList(
                        new LambdaQueryWrapper<WarehouseBillDetail>()
                                .eq(WarehouseBillDetail::getCustomerId, customerId)
                                .in(WarehouseBillDetail::getSysBundleCode,sysBundleCodeList)
                                .eq(WarehouseBillDetail::getBillPeriod, DateUtils.getYearMonth())
                                .ne(WarehouseBillDetail::getTransferType,3));
        warehouseBillDetailList.forEach(warehouseBillDetail -> {
            warehouseBillDetail.setEndDate(LocalDateTime.now());
        });
       return updateBatchById(warehouseBillDetailList);
    }

    @Override
    public Boolean addBillDetail(Long customerId, List<BillDetailInfo> sysBundleCodeList , Long billId){
        List<WarehouseBillDetail> warehouseBillDetailList = sysBundleCodeList.stream().map(billDetailInfo -> {
            WarehouseBillDetail warehouseBillDetail = new WarehouseBillDetail();
            warehouseBillDetail.setCustomerId(customerId);
            warehouseBillDetail.setWarehouseBillId(billId);
            warehouseBillDetail.setSysBundleCode(billDetailInfo.getSysBundleCode());
            warehouseBillDetail.setTransferType(billDetailInfo.getTransferType());
            warehouseBillDetail.setBillPrice(billDetailInfo.getPrice());
            warehouseBillDetail.setBillPeriod(DateUtils.getYearMonth());
            //修改为交割日期
            if(null!=billDetailInfo.getHandoverTime()){
                warehouseBillDetail.setStartDate(billDetailInfo.getHandoverTime());
            }else{
                warehouseBillDetail.setStartDate(LocalDateTime.now());
            }
            warehouseBillDetail.setBillStatus(BillStatusEnum.UNPAID.getCode());
            return warehouseBillDetail;
        }).collect(Collectors.toList());
        return saveBatch(warehouseBillDetailList);
    }

    @Override
    public Boolean addBillDetailOrder(Long customerId, Long billId,Double totalProcessingFee){
        WarehouseBillDetail warehouseBillDetail = new WarehouseBillDetail();
        warehouseBillDetail.setCustomerId(customerId);
        warehouseBillDetail.setWarehouseBillId(billId);
        warehouseBillDetail.setTransferType(3);
        warehouseBillDetail.setBillPrice(totalProcessingFee);
        warehouseBillDetail.setBillPeriod(DateUtils.getYearMonth());
        warehouseBillDetail.setStartDate(LocalDateTime.now());
        return save(warehouseBillDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAllBillDetail() {
        List<WarehouseBillDetail> warehouseBillDetailList = baseMapper
                .selectList(
                        new LambdaQueryWrapper<WarehouseBillDetail>()
                                .eq(WarehouseBillDetail::getBillPeriod, DateUtils.getLastMonth()));
        warehouseBillDetailList.forEach(warehouseBillDetail -> {
            warehouseBillDetail.setEndDate(LocalDateTime.now());
        });
        System.out.println("============"+DateUtils.getYearMonth());
        System.out.println("============"+DateUtils.getLastMonth());
        return updateBatchById(warehouseBillDetailList);
    }

    @Override
    public Boolean updateBillDetailByBillId(Long id) {
        LambdaUpdateWrapper<WarehouseBillDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(WarehouseBillDetail::getBillStatus, BillStatusEnum.PAID.getCode())
                .eq(WarehouseBillDetail::getWarehouseBillId, id);
        return this.update(null, updateWrapper);
    }


}
